package com.sixth.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixth.core.entity.Producer;
import com.sixth.core.util.WebUtil;
import com.sixth.core.vo.R;
import com.sixth.stock.mapper.ProducerMapper;
import com.sixth.stock.service.ProducerService;
import com.sixth.stock.vo.ProducerVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【stock_producer(生产厂家表)】的数据库操作Service实现
* @createDate 2024-08-14 15:46:59
*/
@Service
public class ProducerServiceImpl extends ServiceImpl<ProducerMapper, Producer>
    implements ProducerService{
   @Resource
   private ProducerMapper producerMapper;
    @Override
    public R<IPage<Producer>> listByPage(ProducerVo producerVo)  {
        IPage<Producer> page=new Page<>(producerVo.getPageNum(), producerVo.getPageSize());
        QueryWrapper<Producer> wrapper=new QueryWrapper<>();
        if(producerVo.getProducerName()!=null){
            wrapper.like("producer_name",producerVo.getProducerName());
        }
        //电话包含输入的参数
        if(producerVo.getProducerTel()!=null){
            wrapper.like("producer_tel",producerVo.getProducerTel());
        }
        if (producerVo.getKeywords()!=null){
            wrapper.like("keywords",producerVo.getKeywords());
        }
        if (producerVo.getStatus()!=null&&!producerVo.getStatus().equals("")){
            wrapper.eq("status",producerVo.getStatus());
        }
        if (producerVo.getStatus()==null||producerVo.getStatus().equals("")){
            wrapper.eq("status","0");
        }
        if (producerVo.getDateRange()!=null&&producerVo.getDateRange().size()==2){
            wrapper.between("create_time",producerVo.getDateRange().get(0),producerVo.getDateRange().get(1));
        }
        wrapper.orderByDesc("create_time");
        IPage<Producer> producerIPage = producerMapper.selectPage(page, wrapper);
        return R.ok(producerIPage);
    }
    @Override
    public R batchDelete(Long[] producerIds)  {
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < producerIds.length; i++) {
            Producer producer = producerMapper.selectById(producerIds[i]);
            if (producer!=null){
                producer.setStatus("1");
                int i1 = producerMapper.updateById(producer);
                sb=sb.append(producerIds[i]+":"+i1+";");
            }
        }
        return R.ok(sb.toString());
    }

    @Override
    public R add(Producer producer) {
        String username = WebUtil.getUsername();
        producer.setCreateBy(username);
        producer.setCreateBy(String.valueOf(new Date()));
        producer.setStatus("0");
        return R.ok(producerMapper.insert(producer));
    }

    @Override
    public R update(Producer producer) {
        producer.setUpdateBy(String.valueOf(new Date()));
        String username = WebUtil.getUsername();
        producer.setUpdateBy(username);
        return R.ok(producerMapper.updateById(producer));
    }

    @Override
    public R deleteById(Long id) {
        Producer producer = producerMapper.selectById(id);
        producer.setStatus("1");
        return R.ok(producerMapper.updateById(producer));
    }

    @Override
    public R listAll() {
        QueryWrapper<Producer> wrapper=new QueryWrapper<>();
        wrapper.eq("status","0");
        List<Producer> list = producerMapper.selectList(wrapper);
        return R.ok(list);
    }
}




